<?php


namespace app\index\controller\xilujob;


use app\common\model\xilujob\Area;
use app\common\controller\xilujob\Xilujob;
use app\common\model\xilujob\Benefit;
use app\common\model\xilujob\CompanyProfile;
use app\common\model\xilujob\CompanyProperties;
use app\common\model\xilujob\CompanyScale;
use app\common\model\xilujob\EnterpriseInfo;
use app\common\model\xilujob\IndustryCategory;
use app\common\model\xilujob\SiteCity;
use app\common\model\xilujob\XilujobUser;
use think\Db;
use think\Exception;

class Entry extends Xilujob
{
    protected $noNeedRight = ['*'];
    protected $noNeedEnterpriseAudit = ['entry'];

    public function entry(){

        if($this->request->isPost()) {
            $xilujobUserInfo = $this->request->post('xilujobuser/a');
            $auditInfo = $this->request->post('audit/a');
            $info = $this->request->post('basic/a');
            $user = $this->auth->getUser();

            Db::startTrans();
            try {
                #1.判断是否已存在身份
                $xilujobuser = XilujobUser::get(['user_id'=>$user->id]);
                if(!$xilujobuser){
                    $xilujobuser = XilujobUser::create(['user_id'=>$user->id]);
                }
                if($xilujobUserInfo['group_id'] == '2'){
                    $xilujobUserInfo['enterprise_group_id'] = XilujobUser::GROUP_COMPANY;
                }
                #2.判断公司是否存在
                $companyProfile = CompanyProfile::get(['user_id'=>$user->id]);
                if(!$companyProfile){
                    $companyProfile = CompanyProfile::create(['user_id'=>$user->id]);
                }
                $xilujobuser->save($xilujobUserInfo);

                #3.判断公司基本信息是否存在
                $enterprise = EnterpriseInfo::get(['company_id'=>$companyProfile->id]);
                if(!$enterprise){
                    $enterprise = EnterpriseInfo::create(['user_id'=>$user->id,'company_id'=>$companyProfile->id]);
                }

                if(!isset($info['full_name']) && empty($enterprise['full_name'])){
                    $info['full_name'] = $auditInfo['company_name'];
                }
                #4.更新资料
                $ret = EnterpriseInfo::submitEnterpriseAuditInfo($companyProfile->id, $auditInfo);
                $ret = EnterpriseInfo::saveInfo($companyProfile->id, $info);
            }catch (Exception $exception){
                Db::rollback();
                $this->error($exception->getMessage());
            }
            Db::commit();

            $this->success();
        }
        $enterpriseinfo = [];
        if($this->company){
            $enterpriseinfo = EnterpriseInfo::get(['company_id'=>$this->company->id]);
            if($enterpriseinfo){
                $enterpriseinfo->environment_images = $enterpriseinfo->environment_images ? explode(',',$enterpriseinfo->environment_images) : [];
            }
        }



        $this->assign('enterpriseinfo', $enterpriseinfo);
        $this->assignconfig('areas', Area::getAreaTree());
        $this->assign('industry_categories', IndustryCategory::getAll());
        $this->assign('company_scales', CompanyScale::getAll());
        $this->assign('company_types', CompanyProperties::getAll());
        $this->assign('benefits', Benefit::getBenefits());
        $this->assign('benefit_ids_arr', !empty($enterpriseinfo->benefit_ids) ? explode(',',$enterpriseinfo->benefit_ids):[]);

        $this->assign('menu_tag', 'enterprise_info');
        $this->assign('title', '企业信息');

        return $this->fetch();
    }

}